﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Xml.Linq;

namespace Lastfm7.ViewModels
{
    public class ArtistViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string artistName;
        private int? playCount;
        private Uri imageUrl;

        public string ArtistName
        {
            get { return artistName; }
            set
            {
                artistName = value;
                PropertyChanged.Raise(this, "ArtistName");
            }
        }

        public Uri ImageUrl
        {
            get { return imageUrl; }
            set
            {
                imageUrl = value;
                PropertyChanged.Raise(this, "ImageUrl");
            }
        }

        public int? PlayCount
        {
            get { return playCount; }
            set
            {
                playCount = value;
                PropertyChanged.Raise(this, "PlayCount");
            }
        }


        public ArtistViewModel() { }

        public ArtistViewModel(XElement artist)
        {
            ArtistName = artist.Element("name").Value;

            var largeImage = (from img in artist.Elements("image")
                              where img.Attribute("size").Value == "mega"
                              select img.Value).SingleOrDefault();

            ImageUrl = new Uri(largeImage);

            var xPlayCount = artist.Element("playcount");
            if (xPlayCount != null)
                PlayCount = int.Parse(xPlayCount.Value);
        }
    }
}
